﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Summerset.Isbndb.Books
{
    /// <summary>
    /// Represents information either about a current or historical price for this publication.
    /// </summary>
    public sealed class BookElementPrice
    {
        /*
         Example response element:
           <Price store_isbn="" 
            store_title=""
            store_url="http://isbndb.com/x/book/thief_of_time/buy/isbn/ecampus.html"
            store_id="ecampus" 
            currency_code="USD" 
            is_in_stock="1" 
            is_new="1"
            check_time="2005-07-29T01:18:18" 
            currency_rate="1" 
            price="5.25"/>
         */

        #region Constructor + Private Fields

        readonly XmlNode _priceNode;

        /// <summary>
        /// Creates a new instance of a <see cref="BookElementPrice"/>.
        /// </summary>
        /// <param name="PriceDetails">The underlying XML information for this instance.</param>
        public BookElementPrice(XmlNode PriceDetails)
        {

            if (!string.Equals(PriceDetails.Name, "Price", StringComparison.InvariantCultureIgnoreCase))
                throw new ArgumentException("The provided XmlNode was not valid.  This constructor expects an element named 'Price'");

            _priceNode = PriceDetails;

        }

        #endregion
        #region Properties

        /// <summary>
        /// Gets an ISBNdb-specific store identifier.
        /// </summary>
        public string StoreId
        {
            get { return _priceNode.Attributes["store_id"].Value; }
        }

        /// <summary>
        /// Gets the URL of this book at the merchant's site. Note, that oftentimes links go through ISBNdb.com 
        /// redirection and ISBNdb.com receives a small percentage of sales generated through the use of these links. 
        /// Since ISBNdb.com is an independent company such revenue share and advertisements are the only source 
        /// of our income. Please consider using these links.
        /// </summary>
        public Uri StoreUri
        {
            get { return new Uri(_priceNode.Attributes["store_url"].Value); }
        }
        
        /// <summary>
        /// Gets the book title at the merchant if different from ISBNdb.com's title.
        /// </summary>
        public string StoreTitle
        {
            get { return _priceNode.Attributes["store_title"].Value; }
        }

        /// <summary>
        /// Gets the book ISBN at the merchant store if different from ISBNdb.com's
        /// </summary>
        public string StoreIsbn
        {
            get { return _priceNode.Attributes["store_isbn"].Value; }
        }

        /// <summary>
        /// Gets the ISO 4217 currency code for the price.
        /// </summary> 
        public string CurrencyCode
        {
            get { return _priceNode.Attributes["currency_code"].Value; }
        }    

        /// <summary>
        /// Gets the currency rate to USD at the time of price query        
        /// </summary>
        public decimal CurrencyRate
        {
            get { return Decimal.Parse(_priceNode.Attributes["currency_rate"].Value); }
        }

        /// <summary>
        /// Gets a value that indicates whether this is a new or historical price.
        /// </summary>
        public bool IsNew
        {
            get { return Convert.ToBoolean(int.Parse(_priceNode.Attributes["is_new"].Value)); }
        }

        /// <summary>
        /// Gets a value that indicates whether the merchant reports this book as being in stock.
        /// </summary>
        public bool IsInStock
        {
            get { return Convert.ToBoolean(int.Parse(_priceNode.Attributes["is_in_stock"].Value)); }
        }

        /// <summary>
        /// Gets a value that indicates if this is the most current price for this publication.        
        /// </summary>
        public bool IsHistoric
        {
            get { return Convert.ToBoolean(int.Parse(_priceNode.Attributes["is_historic"].Value)); }
        }

        /// <summary>
        /// Gets the date and time when this price record was received from the merchant.
        /// </summary>        
        public DateTimeOffset TimeRetrieved
        {
            get { return DateTimeOffset.Parse(_priceNode.Attributes["check_time"].Value); }
        }

        #endregion
    }
}
